#include "WorldScene.h"
#include "LobbyScene.h"

using namespace cocos2d;


//배경 하나당 거리는 7m로 가정
enum _BACKGROUND_STATUS
{
    BACKGROUND_NORMAL_FRONT,
    BACKGROUND_NORMAL_BACK,
    BACKGROUND_END,
}
BACKGROUND_STATUS;

CCScene* WorldScene::scene()
{
    CCScene * scene = NULL;
    do
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);
        
        // 'layer' is an autorelease object
        WorldScene *layer = WorldScene::create();
        CC_BREAK_IF(! layer);
        
        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool WorldScene::init()
{
    if ( !CCLayer::init() )
	{
		return false;
    }
    
    this->setTouchEnabled(true);
    this->setTouchMode(kCCTouchesAllAtOnce);
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                                          "btn_nor.png",
                                                          "btn_focus.png",
                                                          this,
                                                          menu_selector(WorldScene::enterLobbyCallback));
    
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));
    
    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);
    
    //배경
    CCSprite* backRunningF = CCSprite::create("back1.jpg");
    backRunningF->setTag(BACKGROUND_NORMAL_FRONT);
    backRunningF->setPosition(ccp(0,0));
    
    CCSprite* backRunningB = CCSprite::create("back1.jpg");
    backRunningB->setTag(BACKGROUND_NORMAL_BACK);
    backRunningB->setPosition(ccp(backRunningF->getContentSize().width,0));

    CCSprite* backEnd = CCSprite::create("back_end.png");
    backEnd->setTag(BACKGROUND_END);
    backEnd->setVisible(false);
    
    this->addChild(backRunningF, 10);
    this->addChild(backRunningB, 10);
    this->addChild(backEnd, 10);
    
    //캐릭터
    extension::CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("bros0.png", "bros0.plist", "bros.ExportJson");
    
    user = new GameUser(OT_USER);
	user->init();
    user->autorun = false;
	user->armature = cocos2d::extension::CCArmature::create("bros");
	user->armature->setPosition(ccp(300,130));
	user->armature->setScale(1.0f);
	user->status = CHAR_READY_START;
    
	this->addChild(user->armature, 20);
    
    this->schedule(schedule_selector(WorldScene::tick));
    
    return true;
}

void WorldScene::startGame()
{
    movingDist = 0.0;
}

void WorldScene::tick(float dt)
{
    user->curSpeed -= 200 * dt;
    user->update(dt);
    
    long t = getMillisecond();
    
    if(jumpTick != 0 && (t - jumpTick) > 500)
    {
        if(user->status == CHAR_RUN)
        {
            jump();
        }
    }
    
    updateBackgroud(dt);
}

void WorldScene::updateBackgroud(float dt)
{
    float speed = (int)(dt * user->curSpeed);
    
    CCSprite *front = (CCSprite *)this->getChildByTag(BACKGROUND_NORMAL_FRONT);
	CCSprite *back = (CCSprite *)this->getChildByTag(BACKGROUND_NORMAL_BACK);
    
	CCPoint frontPos = front->getPosition();
	CCPoint backPos = back->getPosition();
    
	frontPos.x -= speed;
	backPos.x -= speed;
    
	if (frontPos.x < -(front->getContentSize().width))
	{
		frontPos.x += front->getContentSize().width;
		backPos.x += front->getContentSize().width;
	}
    
	frontPos.x = (int)frontPos.x;
	backPos.x = (int)backPos.x;
	front->setPosition( frontPos );
	back->setPosition( backPos );
    
    movingDist += speed;
}

void WorldScene::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
    jumpTick = getMillisecond();
}

void WorldScene::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
    
}

void WorldScene::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
    jumpTick = 0;
    
    CCSetIterator it = pTouches->begin();
    
    while(it != pTouches->end())
    {
        CCTouch* touch = (CCTouch*)*it;
        
        CCPoint location = touch->getLocation();
        
        CCParticleSmoke* pParticle = CCParticleSmoke::create();
        pParticle->setPositionX(location.x);
        pParticle->setPositionY(location.y);
        pParticle->setDuration(1);
        pParticle->setAutoRemoveOnFinish(true);
        this->addChild(pParticle, 100);
        
        it++;
    }
    
    run();
}

void WorldScene::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent)
{
    
}

long WorldScene::getMillisecond()
{
    struct cc_timeval now;
    CCTime::gettimeofdayCocos2d(&now, NULL);
    
    return (now.tv_sec * 1000 + now.tv_usec / 1000);
}

void WorldScene::run()
{
    switch(user->status)
    {
        case CHAR_RUN :
        case CHAR_WALK :
            user->curSpeed += 50;
            break;
        case CHAR_READY_END :
        case CHAR_BREATH_END :
            break;
        case CHAR_IDLE :
            user->curSpeed += 100;
            user->status = CHAR_RUN_START;
            break;
        default:
            break;
    }
}

void WorldScene::jump()
{
    user->status = CHAR_RUNJUMP_START;
    jumpTick = 0;
}

void WorldScene::enterLobbyCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->replaceScene(CCTransitionSplitCols::create(1.0, LobbyScene::scene()));
}

